//
// Created by zhengjuping on 24-9-9.
//

#ifndef ENV_II_BT_GATTSSERVICE_H
#define ENV_II_BT_GATTSSERVICE_H

#include <bitset>
#include "BleUuid.h"
#include "memory"
#include "GattCharacteristic.h"
#include "list"
#include "vector"

#define GATT_ATTR_HANDLE_MAX 5

class GattsService {
public:
    GattsService( const BleUuid &serviceUuid ) ;
    virtual ~GattsService() = default;

    int32_t addCharacteristic( const std::shared_ptr<GattCharacteristic> &characteristic,
                               const std::shared_ptr<GattCharacteristic> &descCharacteristic); //return idx

    int32_t characteristicReadEvent( uint16_t handle, uint8_t *p_value, uint16_t &length );
    int32_t characteristicWriteEvent( uint16_t handle, uint8_t *p_value, uint16_t length );
    int32_t characteristicNotifyEvent( uint16_t handle, bool enable );
    int32_t characteristicConnectEvent(uint16_t gattsIf, uint16_t connId);
    int32_t characteristicDisconnectEvent(uint16_t gattsIf, uint16_t connId);

    void pushHandles( const uint16_t* handles, uint16_t numHandles );
    bool hasHandle( uint16_t handle );

    void setSvcId( int32_t id );
    int32_t getSvcId() const;

    const BleUuid &getServiceUuid() const;
    const esp_gatts_attr_db_t *getAttributeTable( uint32_t &size ) const;

    void dump();

private:
    BleUuid _serviceUuid;
    uint16_t _serviceHandle;
    int32_t _serviceId;

    std::vector<std::shared_ptr<GattCharacteristic>> _characteristicsVector;

    std::vector<esp_gatts_attr_db_t> _attributeTableVector;

    int32_t _attributeTableSize;
    uint32_t _primary_service_uuid = 0x00FF;
    uint32_t _primary_service_uuid_size = 2;
};


#endif //ENV_II_BT_GATTSSERVICE_H
